package cn.ucox.web.ms.service;

import com.dragonsoft.adapter.service.QueryAdapterSend;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class IRkkService {
    public Map<String, Object> getAuthication(String userName, String idNum) {
        try {
            if (org.springframework.util.StringUtils.isEmpty(userName) || org.springframework.util.StringUtils.isEmpty(idNum)) {
                Map<String, Object> map = new HashedMap();
                map.put("status", "-1");
                map.put("HKSZD", "");
                map.put("souce", "remote");
                map.put("error", "身份证号码或者姓名为空");
                return map;
            }
            String condition = "(XM='" + userName + "') AND (SFZH='" + idNum + "')";
            String strReturns = null;
            QueryAdapterSend adapter = new QueryAdapterSend();
            strReturns = adapter.sendQuery("QueryZZRK", condition, idNum, userName, "010000");
            if (strReturns == null || strReturns == "") {
                Map<String, Object> map = new HashedMap();
                map.put("status", "-1");
                map.put("HKSZD", "");
                map.put("souce", "remote");
                map.put("error", "返回值为空");
                return map;
            }
            return getResult(strReturns);
        } catch (Exception e) {
            //logger.error("调用全国人口库失败。原因：" + e.toString());
            Map<String, Object> map = new HashedMap();
            map.put("status", "-1");
            map.put("HKSZD", "");
            map.put("souce", "remote");
            map.put("error", "调用全国人口库失败。原因：" + e.toString());
            return map;
        }
    }
    private Map<String, Object> getResult(String strxml) {
        Map<String, Object> map = new HashedMap();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(strxml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            List<Element> childList = rootElt.element("Method").element("Items").element("Item").element("Value").elements("Row");
            if (childList == null || childList.size() < 3) {
                throw new DocumentException("");
            }
            List<Element> dataList = childList.get(2).elements("Data");
            if (StringUtils.isEmpty(dataList.get(0).getText())) {
                throw new DocumentException("");
            } else {
                map.put("status", "1");
                map.put("HKSZD", dataList.get(6).getText());
                map.put("XP", dataList.get(10).getText());
            }

        } catch (DocumentException e) {
            e.printStackTrace();
            map.put("status", "-1");
            map.put("HKSZD", "");
            map.put("XP", "");
        }
        map.put("souce", "remote");
        return map;
    }
}
